﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SwaggerJwtAuthenticationCenter.DbHelper.Base;
using SwaggerJwtAuthenticationCenter.Helper;
using SwaggerJwtAuthenticationCenter.Models;
using SwaggerJwtAuthenticationCenter.Models.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SwaggerJwtAuthenticationCenter.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly ILoginService _loginService;

        public LoginController(ILoginService loginService)
        {
            _loginService = loginService;
        }
        /// <summary>
        /// 统一登录接口,登录成功，获取AccessToken
        /// </summary>
        /// <param name="loginModel">登录模型</param>
        /// <returns></returns>
        [HttpGet("Login")]
        [AllowAnonymous]
        public dynamic Login([FromQuery]LoginModel loginModel)
        {
            var result = _loginService.Login(loginModel);
            if(result.result)
            {
                var resultData = result.data;
               string token= JwtHelper.IssueJwt(new TokenModelJwt { Uid = loginModel.userCode, Name=loginModel.userName, Role = resultData.role,Department=resultData.department });
                return new ResponseMsg<string> {result=true, message="获取Token成功", data=token };
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <returns></returns>
        [HttpPost("AddUser")]
        [Authorize(Roles ="super")]
        public dynamic AddUser([FromBody]UserApplication userApplication)
        {

            return new {code=1322,name="1212" }; 
        }
    }
}
